﻿#include "itemwidget.h"
#include "ui_itemwidget.h"

ItemWidget::ItemWidget(QWidget *parent,
                       const QString &table_,
                       const QString &category_) :
    BaseEditor(parent),
    ui(new Ui::ItemWidget)
{
    ui->setupUi(this);

    selectId=0;
    table=table_;
    category=category_;

    model=new QSqlTableModel(this);
    ui->tableView->setModel(model);

    createMenu();
    updateView();   //手动更新
}

ItemWidget::~ItemWidget()
{
    delete ui;
}

void ItemWidget::keyPressEvent(QKeyEvent *event)
{
    if(event->modifiers() == Qt::ControlModifier && event->key()==Qt::Key_S)
        save();
    else if(event->modifiers() == Qt::ControlModifier && event->key()==Qt::Key_R)
        do_cancelCategory();
    else
    {
        switch (event->key()) {
        case Qt::Key_F1:
            newItem();
            break;
        case Qt::Key_F3:
            do_changeStatus();
            break;
        case Qt::Key_F5:
            updateView();
            break;
        }
    }
}

void ItemWidget::generateMenu()
{
    menu->addAction(QStringLiteral("新记录... (F1)"),this,&ItemWidget::newItem);
    menu->addSeparator();
    menu->addAction(QStringLiteral("更新视图 (F5)"),this,&ItemWidget::updateView);
    menu->addSeparator();
    menu->addAction("取消" + category + " (Ctrl+R)",this,&ItemWidget::do_cancelCategory);
    menu->addAction(QStringLiteral("编辑备注 (F3)"),this,&ItemWidget::do_changeStatus);
    menu->addSeparator();
    menu->addAction(QStringLiteral("保存 (Ctrl+S)"),this,&ItemWidget::save);
}

void ItemWidget::save()
{
    if(model->rowCount() < 1)
        return;

    if(selectId <= 0)
        return;

    QString detail=ui->detail->toPlainText();
    if(detail.isEmpty() || detail == previousDetail)
        return;

    if(sp_updateColumnById(table,"detail",detail,selectId))
        updateView();
    else
        MESSAGE_CANNOT_SAVE
}

void ItemWidget::do_changeStatus()
{
    if(model->rowCount() < 1)
        return;

    if(selectId <= 0)
        return;

    QString status=QInputDialog::getText(this,QStringLiteral("更新备注"),
                                         QStringLiteral("输入："));
    if(status.isEmpty())
        return;

    if(status == FINISH_FLAG)
    {
        sp_updateColumnById(table,"month_",CurrentMonth,selectId);
        sp_updateColumnById(table,"date_",Today,selectId);
        sp_updateColumnById(table,"category",CAT_JOURNAL,selectId);
    }

    if(sp_updateColumnById(table,"addition",status,selectId))
        updateView();
    else
        MESSAGE_CANNOT_UPDATE
}

void ItemWidget::do_cancelCategory()
{
    if(model->rowCount() < 1)
        return;

    if(selectId <= 0)
        return;

    if(sp_updateColumnById(table,"category",CAT_OTHER,selectId))
        updateView();
    else
        MESSAGE_CANNOT_UPDATE
}

void ItemWidget::newItem()
{
    IW_AddItem item(this,table,category);
    item.setWindowTitle(QStringLiteral("新") + category);
    if(item.exec() == QDialog::Accepted)
        updateView();
}

void ItemWidget::updateView()
{
    if(category.isEmpty())
        return;

    model->setTable(table);
    model->setFilter(QString("category='%1'").arg(category));
    model->select();

    ui->tableView->hideColumn(ID_COL);   //id
    ui->tableView->hideColumn(MONTH_COL);   //month_
    model->setHeaderData(DATE_COL,Qt::Horizontal,QStringLiteral("日期")); //date_
    ui->tableView->hideColumn(CAT_COL);     //category
    model->setHeaderData(DET_COL,Qt::Horizontal,category); //detail
    ui->tableView->setColumnWidth(DET_COL,800);
    model->setHeaderData(ADD_COL,Qt::Horizontal,QStringLiteral("备注")); //addition

    if(model->rowCount() > 0)
        ui->tableView->resizeRowsToContents();

    ui->detail->clear();
}

void ItemWidget::on_tableView_clicked(const QModelIndex &index)
{
    if(index.isValid())
    {
        selectId=sp_fetchId(index);
        selectDate=sp_fetchString(index,DATE_COL);
        previousDetail=sp_fetchString(index,DET_COL);
        ui->detail->setPlainText(previousDetail);
    }
}

void ItemWidget::on_tableView_doubleClicked(const QModelIndex &)
{
    if(previousDetail.isEmpty())
        return;

    qApp->clipboard()->setText(selectDate + " " + previousDetail);
}
